import {
	ADD_TAB,
	REDUCE_TAB,
	SET_ALL_TAB
} from '../constant'
import {
	cloneDeep
} from 'lodash'
const initState = []

// eslint-disable-next-line import/no-anonymous-default-export
export default function(preState = initState,action){
	const {type,data} = action
	switch (type) {
		case ADD_TAB: //若是添加一个 tab
			const sign = preState.some(r => r.path === data.path)
			//preState.unshift(data) //此处不可以这样写，这样会导致preState被改写了，personReducer就不是纯函数了。
			return sign ? [...preState] : [...preState, data]
		case REDUCE_TAB: // 去掉一个 tab
			const index = preState.findIndex(r => r.path === data.path)
			console.log('查看 index   ==== ',  index)
			let list = cloneDeep(preState)
			list.splice(index, 1)
			return [...list]
		case SET_ALL_TAB:
			return [...data]
		default:
			return preState
	}
}